﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// company_photo_more 的摘要说明
    /// </summary>
    public class company_photo_more : IHttpHandler
    {
        protected int totalCount = 0;
        protected int pages = 1;
        public void ProcessRequest(HttpContext context)
        {
            int pages = DTRequest.GetQueryInt("page", 1);

            int _company_id = DTRequest.GetQueryInt("company_id");
            if (_company_id == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"错误提示：参数不正确！\"}");
                return;
            }
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");

            //获取相册列表
            BLL.xiangce bll5 = new BLL.xiangce();
            DataSet ds5 = bll5.GetList(9, pages, "company_id=" + _company_id, "sort_id desc,add_time desc", out this.totalCount);
            int total_page = this.totalCount / 9;
            if ((this.totalCount % 9) > 0) { total_page += 1; }
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds5.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"xiangce_list\":[");
                for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds5.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    string xiangceimg = dr["img_url"].ToString();

                    if (xiangceimg.Contains("shicaiquanzi"))
                    {
                        if (xiangceimg.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + xiangceimg + "\""); }
                        else { strTxt.Append(",\"img_url\":\"http://" + xiangceimg + "\""); }

                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + xiangceimg + "\"");
                    }

                
                    Model.xiangce modelXiangce = bll5.GetModel(int.Parse(dr["id"].ToString()));
                    if (modelXiangce.albums != null)
                    {
                        strTxt.Append(",\"pic_list\":[");
                        int ii = 0;
                        foreach (Model.common_albums modelt in modelXiangce.albums)
                        {
                            ii++;

                            if (modelt.original_path.Contains("shicaiquanzi"))
                            {
                                if (xiangceimg.Contains("http://")) { strTxt.Append("{\"picurl\":\"" + modelt.original_path + "\"}"); }
                                else { strTxt.Append("{\"picurl\":\"http://" + modelt.original_path + "\"}"); }

                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"http://pic.shicaiquanzi.com" + modelt.original_path + "\"}");
                            }

                       
                            //是否加逗号
                            if (ii < modelXiangce.albums.Count)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds5.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"xiangce_list\":[]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}